<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Add an insert operation to the bulk</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mongodb-driver-bulkwrite.delete.html">« MongoDB\Driver\BulkWrite::delete</a></li>
      <li style="float: right;"><a href="mongodb-driver-bulkwrite.update.html">MongoDB\Driver\BulkWrite::update »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.mongodb-driver-bulkwrite.html">MongoDB\Driver\BulkWrite</a></li>
    <li>Add an insert operation to the bulk</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mongodb-driver-bulkwrite.insert" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoDB\Driver\BulkWrite::insert</h1>
  <p class="verinfo">(mongodb &gt;=1.0.0)</p><p class="refpurpose"><span class="refname">MongoDB\Driver\BulkWrite::insert</span> &mdash; <span class="dc-title">Add an insert operation to the bulk</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongodb-driver-bulkwrite.insert-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>MongoDB\Driver\BulkWrite::insert</strong></span>(<span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type">object</span></span> <code class="parameter">$document</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   Adds an insert operation to the
   <span class="classname"><a href="class.mongodb-driver-bulkwrite.html" class="classname">MongoDB\Driver\BulkWrite</a></span>.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-mongodb-driver-bulkwrite.insert-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">document</code> (<span class="type"><span class="type">array</span>|<span class="type">object</span></span>)</dt>

    <dd>

     <p class="para">
      A document to insert.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-mongodb-driver-bulkwrite.insert-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the <code class="literal">_id</code> of the inserted document. If the
   <code class="parameter">document</code> did not have an <code class="literal">_id</code>, the
   <span class="classname"><a href="class.mongodb-bson-objectid.html" class="classname">MongoDB\BSON\ObjectId</a></span> generated for the insert will
   be returned.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-mongodb-driver-bulkwrite.insert-errors">
  <h3 class="title">错误／异常</h3>
  <ul class="simplelist">
   <li class="member">Throws <span class="classname"><a href="class.mongodb-driver-exception-invalidargumentexception.html" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a></span> on argument parsing errors.</li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-mongodb-driver-bulkwrite.insert-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>PECL mongodb 1.3.0</td>
       <td>
        The <code class="literal">_id</code> of the inserted document is always returned.
        Previously, the method only returned a value if a
        <span class="classname"><a href="class.mongodb-bson-objectid.html" class="classname">MongoDB\BSON\ObjectId</a></span> was generated.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mongodb-driver-bulkwrite.insert-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1315">
   <p><strong>示例 #1 <span class="function"><strong>MongoDB\Driver\BulkWrite::insert()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$bulk&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Driver</span><span style="color: #007700">\</span><span style="color: #0000BB">BulkWrite</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$document1&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'title'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'one'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$document2&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'_id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'custom&nbsp;ID'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'title'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'two'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$document3&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'_id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">ObjectId</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'title'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'three'</span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">$_id1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$bulk</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #0000BB">$document1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$_id2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$bulk</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #0000BB">$document2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$_id3&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$bulk</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #0000BB">$document3</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$_id1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$_id2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$_id3</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$manager&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Driver</span><span style="color: #007700">\</span><span style="color: #0000BB">Manager</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://localhost:27017'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$manager</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">executeBulkWrite</span><span style="color: #007700">(</span><span style="color: #DD0000">'db.collection'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$bulk</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
object(MongoDB\BSON\ObjectId)#3 (1) {
  [&quot;oid&quot;]=&gt;
  string(24) &quot;54d51146bd21b91405401d92&quot;
}
NULL
NULL
</pre></div>
   </div>
  </div>
 </div>



 <div class="refsect1 seealso" id="refsect1-mongodb-driver-bulkwrite.insert-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="methodname"><a href="mongodb-driver-manager.executebulkwrite.html" class="methodname" rel="rdfs-seeAlso">MongoDB\Driver\Manager::executeBulkWrite()</a> - Execute one or more write operations</span></li>
   <li class="member"><span class="classname"><a href="class.mongodb-driver-writeresult.html" class="classname">MongoDB\Driver\WriteResult</a></span></li>
   <li class="member"><span class="classname"><a href="class.mongodb-bson-objectid.html" class="classname">MongoDB\BSON\ObjectId</a></span></li>
   <li class="member"><span class="interfacename"><a href="class.mongodb-bson-persistable.html" class="interfacename">MongoDB\BSON\Persistable</a></span></li>
  </ul>
 </div>


</div></div></div></body></html>